package lpl.com.xyd.test.manage.impl;

import com.alibaba.fastjson.JSONObject;
import lpl.com.xyd.test.manage.common.enums.ProjectStatusEnum;
import lpl.com.xyd.test.manage.common.response.HttpResponse;
import lpl.com.xyd.test.manage.convert.ProjectConvert;
import lpl.com.xyd.test.manage.domain.ProjectDomain;
import lpl.com.xyd.test.manage.facade.ProjectFacade;
import lpl.com.xyd.test.manage.factory.ProjectDomainFactory;
import lpl.com.xyd.test.manage.info.ProjectInfo;
import lpl.com.xyd.test.manage.order.ProjectAddOrder;
import lpl.com.xyd.test.manage.order.ProjectQueryOrder;
import lpl.com.xyd.test.manage.order.ProjectUpdateOrder;
import lpl.com.xyd.test.manage.param.ProjectParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectFacadeImpl implements ProjectFacade {

    private final static Logger LOGGER = LoggerFactory.getLogger(ProjectFacadeImpl.class);

    @Autowired
    private ProjectDomainFactory projectDomainFactory;

    @Override
    public HttpResponse<ProjectInfo> addProject(ProjectAddOrder order) {
        LOGGER.info("新增项目，order:{}", JSONObject.toJSONString(order));
        HttpResponse<ProjectInfo> response = new HttpResponse<>();
        ProjectParam param = new ProjectParam();
        BeanUtils.copyProperties(order,param);
        param.setStatus(order.getStatus().getCode());
        ProjectDomain domain = projectDomainFactory.create(param);
        Boolean add = projectDomainFactory.store(domain);
        if (!add){
            response.failfure();
            response.setMessage("新增项目失败");
            return response;
        }
        response.successful();
        response.setMessage("新增成功");
        return response;
    }

    @Override
    public HttpResponse<List<ProjectInfo>> queryList(ProjectQueryOrder order) {
        LOGGER.info("查询项目列表，order:{}", JSONObject.toJSONString(order));
        HttpResponse<List<ProjectInfo>> response = new HttpResponse<>();
        ProjectParam param = new ProjectParam();
        BeanUtils.copyProperties(order,param);
        List<ProjectDomain> domains = projectDomainFactory.activeList(param);
        if(null == domains || domains.size() <= 0){
            response.failfure();
            response.setMessage("没有数据");
            return response;
        }
        List<ProjectInfo> projectInfos = new ArrayList<>(domains.size());
        domains.forEach(v->{
            ProjectInfo info = ProjectConvert.convert(v);
            projectInfos.add(info);
        });
        response.successful();
        response.setData(projectInfos);
        return response;
    }

    @Override
    public HttpResponse<ProjectInfo> update(ProjectUpdateOrder order) {
        LOGGER.info("项目修改，order:{}", JSONObject.toJSONString(order));
        HttpResponse<ProjectInfo> response = new HttpResponse<>();
        ProjectDomain domain = projectDomainFactory.active(order.getId().toString());
        domain.modify(order);
        projectDomainFactory.reStore(domain);
        response.successful();
        response.setMessage("修改成功！");
        return response;
    }
}
